package edu.msg.jthr.web.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.exception.ServiceException;
import edu.msg.jthr.backend.model.HiredCandidate;
import edu.msg.jthr.backend.model.Job;
import edu.msg.jthr.backend.model.RecommendedCandidate;
import edu.msg.jthr.backend.model.User;
import edu.msg.jthr.backend.service.CandidateService;
import edu.msg.jthr.backend.service.HiredCandidateService;
import edu.msg.jthr.backend.service.JobService;
import edu.msg.jthr.backend.service.RecommendedCandidateService;
import edu.msg.jthr.backend.service.UserService;
import edu.msg.jthr.web.util.RoleAssembler;

/**
 * Servlet implementation class ShowRecommendedCandidates
 */
@WebServlet("/ShowRecommendedCandidates")
public class ShowRecommendedCandidates extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	private CandidateService candidateService;
	@EJB
	private JobService jobService;
	@EJB
	private RecommendedCandidateService recommendedCandidateService;
	@EJB
	private HiredCandidateService hiredCandidateService;
	@EJB
	UserService userService;
	private HttpSession session;
	private static Logger LOG = LoggerFactory.getLogger(ShowRecommendedCandidates.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShowRecommendedCandidates() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		User user = (User) session.getAttribute("activeUser");
		if (user == null) {
			user = userService.getUserByUserName(request.getUserPrincipal()
					.toString());
			String role = RoleAssembler.getRole(user.getUserGroups());
			session.setAttribute("role", role);
			session.setAttribute("activeUser", user);
		}
		Job job = (Job) session.getAttribute("job");
		if (job == null) {
			String pagePath = "/ShowJobs";
			request.getRequestDispatcher(pagePath).forward(request, response);
		} else {
			job = jobService.update(job);
			session.setAttribute("job", job);
			session.setAttribute("recomCandidateList",
					job.getRecomendedCandidates());
			request.getRequestDispatcher("/pages/ShowRecommendedCandidates.jsp")
					.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		Job job = (Job) session.getAttribute("job");
		if (request.getParameter("hire") != null) {
			RecommendedCandidate reCa = new RecommendedCandidate();
			try {
				reCa = recommendedCandidateService
						.getById(Long.parseLong(request
								.getParameter("reCandidateId")));
			} catch (ServiceException e) {
				LOG.error(e.getMessage(), e);
			}
			
			job.getRecomendedCandidates().remove(reCa);
			HiredCandidate hiredCandidate = new HiredCandidate(reCa);
			try {
				recommendedCandidateService.delete(reCa);
				
				job.getHiredCandidates().add(hiredCandidate);
				hiredCandidateService.create(hiredCandidate);
			} catch (ServiceException e) {
				LOG.error(e.getMessage(), e);
			}
			
			Integer maxCand = job.getMaxCandidates();
			if (maxCand > 0) {
				job.setMaxCandidates(--maxCand);
				try {
					jobService.update(job);
					hiredCandidate.getCandidate().setHired(true);
					candidateService.update(hiredCandidate.getCandidate());
				} catch (ServiceException e) {
					LOG.error(e.getMessage(), e);
				}
				
			}
			if (job.getMaxCandidates() == 0) {
				job.setOpened(false);
			}
			doGet(request, response);
		}
		if (request.getParameter("dismiss") != null) {
			
			RecommendedCandidate reCa = recommendedCandidateService
					.getById(Long.parseLong(request
							.getParameter("reCandidateId")));
			job.getRecomendedCandidates().remove(reCa);
			recommendedCandidateService.delete(reCa);
			jobService.update(job);
			reCa.getCandidate().setHired(false);
			
			try {
				candidateService.update(reCa.getCandidate());
			} catch (ServiceException e) {
				LOG.error(e.getMessage(), e);
			}
			
			doGet(request, response);

		}
	}
}
